Computer Graphics
|
3D Viewing –
Sample Programs
|
Special Case -- zvp = zp = 0
xprp=0.0 yprp=0.0 zprp=-3.0 |
xprp=1.0 yprp=0.0 zprp=-3.0 |
xprp=1.5 yprp=0.5 zprp=-3.0 |
import java.awt.*;
import java.awt.event.*;
import net.java.games.jogl.*;
public class Cube3
{
static
class Renderer implements GLEventListener,KeyListener
{
private
float rquad = 0.0f;
public
void display(GLDrawable gLDrawable)
{
final
GL gl = gLDrawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
float [][] cube = { {1.0f,
1.0f,1.0f},{-1.0f, 1.0f, 1.0f},
{-1.0f,
1.0f, 3.0f},{1.0f, 1.0f, 3.0f},
{1.0f,-1.0f,
3.0f},{-1.0f,-1.0f, 3.0f},
{-1.0f,-1.0f,1.0f},{1.0f,-1.0f,1.0f}};
float [][] cuber = new float [8][2];
float xprp=1.5f;
float yprp=1.5f;
float zprp=-3.0f;
// Perspective Projection
for (int i =0; i<8;i++){
cuber[i][0]
= cube[i][0] - cube[i][2]*((cube[i][0]-xprp)/(cube[i][2]-zprp));
cuber[i][1]
= cube[i][1] - cube[i][2]*((cube[i][1]-yprp)/(cube[i][2]-zprp));
System.out.println(i+"
"+cuber[i][0]+" "+cuber[i][1]);
}
gl.glLineWidth (2.0f);
gl.glBegin(GL.GL_LINES);
gl.glColor3f(0.0f,1.0f,0.0f); //
Set The Color To Green
gl.glVertex2fv(cuber[0]); //
Top Right Of The Quad (Top), 0
gl.glVertex2fv(cuber[1]); //
Top Left Of The Quad (Top), 1
gl.glVertex2fv(cuber[1]); //
Top Left Of The Quad (Top), 1
gl.glVertex2fv(cuber[2]); //
Bottom Left Of The Quad (Top), 2
gl.glVertex2fv(cuber[2]); //
Bottom Left Of The Quad (Top), 2
gl.glVertex2fv( cuber[3]); //
Bottom Right Of The Quad (Top), 3
gl.glVertex2fv(cuber[3]); //
Bottom Right Of The Quad (Top), 3
gl.glVertex2fv(cuber[0]); // Top Right Of The Quad (Top), 0
gl.glColor3f(1.0f,0.5f,0.0f); //
Set The Color To Orange
gl.glVertex2fv( cuber[4]); //
Top Right Of The Quad (Bottom), 4
gl.glVertex2fv(cuber[5]); //
Top Left Of The Quad (Bottom), 5
gl.glVertex2fv(cuber[5]); //
Top Left Of The Quad (Bottom), 5
gl.glVertex2fv(cuber[6]); //
Bottom Left Of The Quad (Bottom), 6
gl.glVertex2fv(cuber[6]); //
Bottom Left Of The Quad (Bottom), 6
gl.glVertex2fv(cuber[7]); //
Bottom Right Of The Quad (Bottom), 7
gl.glVertex2fv(cuber[7]); // Bottom Right Of The Quad (Bottom), 7
gl.glVertex2fv(cuber[4]); // Top Right Of The Quad (Bottom), 4
gl.glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
gl.glVertex2fv(cuber[0]); // Top Right Of The Quad (Top), 0
gl.glVertex2fv(cuber[7]); // Bottom Right Of The Quad (Bottom), 7
gl.glVertex2fv(cuber[6]); //
Bottom Left Of The Quad (Bottom), 6
gl.glVertex2fv(cuber[1]); //
Top Left Of The Quad (Top), 1
gl.glVertex2fv(cuber[5]); //
Top Left Of The Quad (Bottom), 5
gl.glVertex2fv(cuber[2]); //
TBottom Left Of The Quad (Top), 2
gl.glVertex2fv(cuber[4]); //
Top Right Of The Quad (Bottom), 4
gl.glVertex2fv(cuber[3]); //
Bottom Right Of The Quad (Top), 3
gl.glEnd();
gl.glFlush();
}
public void displayChanged(GLDrawable
gLDrawable, boolean modeChanged, boolean deviceChanged)
{
}
public
void init(GLDrawable gLDrawable)
{
final
GL gl = gLDrawable.getGL();
final
GLU glu = gLDrawable.getGLU();
gl.glClearColor(0.0f, 0.0f, 0.0f,0.0f); // Black Background
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D (-1.5f, 1.5f,
-1.5f, 1.5f); // define drawing
area
gLDrawable.addKeyListener(this);
}
public void reshape(GLDrawable gLDrawable,
int x, int y, int width, int height)
{
}
public
void keyPressed(KeyEvent e)
{
if
(e.getKeyCode() == KeyEvent.VK_ESCAPE)
{
System.exit(0);
}
}
public
void keyReleased(KeyEvent e) {}
public
void keyTyped(KeyEvent e) {}
}
public
static void main(String[] args)
{
Frame
frame = new Frame("Lesson 5: 3D Shapes");
GLCanvas
canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
canvas.addGLEventListener(new Renderer());
frame.add(canvas);
frame.setSize(500, 500);
frame.addWindowListener(new WindowAdapter()
{
public
void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.show();
canvas.requestFocus();
}
}
glu.gluPerspective(angle_of_view,
aspect ratio, front clipping plane, rear clipping plane);
glu.gluLookAt(x0,y0,z0,xref,yref,zref,Vx,Vy,Vz);
default:
P0 = P(x0,y0,z0) = (0,0,0)
Pref = (0,0,-1)
Vup_Vector = (Vx,Vy,Vz) = (0,1,0)
import java.awt.*;
import java.awt.event.*;
import net.java.games.jogl.*;
public class Cube2
{
static
Animator animator = null;
static
class Renderer
implements GLEventListener,
KeyListener
{
private
float rquad = 0.0f;
public
void display(GLDrawable gLDrawable)
{
final
GL gl = gLDrawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
float [][]
cube = { {1.0f, 1.0f,-1.0f},{-1.0f, 1.0f, -1.0f},
{-1.0f, 1.0f, 1.0f},{1.0f,
1.0f, 1.0f},
{1.0f,-1.0f,
1.0f},{-1.0f,-1.0f, 1.0f},
{-1.0f,-1.0f,-1.0f},{1.0f,-1.0f,-1.0f}};
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -4.0f);
gl.glRotatef(rquad, 1.0f, 1.0f, 1.0f);
gl.glLineWidth (2.0f);
gl.glBegin(GL.GL_LINES);
gl.glColor3f(0.0f,1.0f,0.0f); //
Set The Color To Green
gl.glVertex3fv(cube[0]); //
Top Right Of The Quad (Top), 0
gl.glVertex3fv(cube[1]); //
Top Left Of The Quad (Top), 1
gl.glVertex3fv(cube[1]); //
Top Left Of The Quad (Top), 1
gl.glVertex3fv(cube[2]); //
Bottom Left Of The Quad (Top), 2
gl.glVertex3fv(cube[2]); //
Bottom Left Of The Quad (Top), 2
gl.glVertex3fv( cube[3]); //
Bottom Right Of The Quad (Top), 3
gl.glVertex3fv( cube[3]); //
Bottom Right Of The Quad (Top), 3
gl.glVertex3fv(cube[0]); // Top Right Of The Quad
(Top), 0
gl.glColor3f(1.0f,0.5f,0.0f); //
Set The Color To Orange
gl.glVertex3fv( cube[4]); //
Top Right Of The Quad (Bottom), 4
gl.glVertex3fv(cube[5]); //
Top Left Of The Quad (Bottom), 5
gl.glVertex3fv(cube[5]); //
Top Left Of The Quad (Bottom), 5
gl.glVertex3fv(cube[6]); //
Bottom Left Of The Quad (Bottom), 6
gl.glVertex3fv(cube[6]); //
Bottom Left Of The Quad (Bottom), 6
gl.glVertex3fv(cube[7]); //
Bottom Right Of The Quad (Bottom), 7
gl.glVertex3fv(cube[7]); // Bottom Right Of The Quad
(Bottom), 7
gl.glVertex3fv( cube[4]); // Top Right Of The Quad
(Bottom), 4
gl.glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
gl.glVertex3fv(cube[0]); // Top Right Of The Quad (Top), 0
gl.glVertex3fv(
cube[7]); // Bottom Right Of
The Quad (Bottom), 7
gl.glVertex3fv(cube[6]); // Bottom Left Of The Quad
(Bottom), 6
gl.glVertex3fv(cube[1]); //
Top Left Of The Quad (Top), 1
gl.glVertex3fv(cube[5]); //
Top Left Of The Quad (Bottom), 5
gl.glVertex3fv(cube[2]); //
Top Right Of The Quad (Top), 0
gl.glVertex3fv( cube[4]); //
Top Right Of The Quad (Bottom), 4
gl.glVertex3fv( cube[3]); //
Bottom Right Of The Quad (Top), 3
gl.glEnd();
gl.glFlush();
rquad +=
0.05f;
}
public void displayChanged(GLDrawable
gLDrawable, boolean modeChanged, boolean deviceChanged)
{
}
public
void init(GLDrawable gLDrawable)
{
final
GL gl = gLDrawable.getGL();
final
GLU glu = gLDrawable.getGLU();
// gl.glShadeModel(GL.GL_SMOOTH); // Enable Smooth Shading
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
gl.glClearDepth(1.0f); // Depth Buffer Setup
gl.glEnable(GL.GL_DEPTH_TEST); // Enables Depth Testing
gl.glDepthFunc(GL.GL_LEQUAL); //
The Type Of Depth Testing To Do
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluLookAt(0.0,0.0,0.0,0.0,0.0,-1.0,0.0,1.0,0.0);
glu.gluPerspective(70.0f, 1.0,
0.0, 20.0);
gLDrawable.addKeyListener(this);
}
public void reshape(GLDrawable gLDrawable,
int x, int y, int width, int height)
{
}
public
void keyPressed(KeyEvent e)
{
if
(e.getKeyCode() == KeyEvent.VK_ESCAPE)
{
animator.stop();
System.exit(0);
}
}
public
void keyReleased(KeyEvent e) {}
public
void keyTyped(KeyEvent e) {}
}
public
static void main(String[] args)
{
Frame
frame = new Frame("Lesson 5: 3D Shapes");
GLCanvas
canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
canvas.addGLEventListener(new Renderer());
frame.add(canvas);
frame.setSize(300, 300);
animator
= new Animator(canvas);
frame.addWindowListener(new WindowAdapter()
{
public
void windowClosing(WindowEvent e)
{
animator.stop();
System.exit(0);
}
});
frame.show();
animator.start();
canvas.requestFocus();
}
}